﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Spider.Ants;
using Spider.Spiders;
using Spider.Spiders.Workers;
using Spider.Visited;
using QqWBSprider = Spider.Spiders.Workers.QqWBSprider;

namespace Spider.Service
{
    public static class SpiderMonitor
    {
        private static Dictionary<string, string> _task = new Dictionary<string, string>();
        /// <summary>
        /// 本次执行任务成功数量
        /// </summary>
        public static int TaskSuccessCount
        {
            get
            {
                int count=0;
                foreach (var t in _task)
                {
                    if (t.Value.Contains("成功"))
                        count += 1;
                }
                return count;
            }
        }
        /// <summary>
        /// 本次执行任务失败数量
        /// </summary>
        public static int TaskFailedCount
        {
            get
            {
                return _task.Count - TaskSuccessCount;
            }
        }
        /// <summary>
        /// 蜘蛛是否在工作
        /// </summary>
        public static bool SpiderIsWorking { get { return SpiderCaptain.GetInstance().IsWorking; } }
        /// <summary>
        /// 蚂蚁是否在工作
        /// </summary>
        public static bool AntIsWorking { get { return AntCaptain.GetInstance().IsWorking; } }
        /// <summary>
        /// 蜘蛛队长停止工作原因
        /// </summary>
        public static string SpiderCaptainStopWorkReason { get { return SpiderCaptain.StopWorkReason; } }
        /// <summary>
        /// 蚂蚁队长停止工作原因
        /// </summary>
        public static string AntCaptainStopWorkReason { get { return AntCaptain.StopWorkReason; } }
        /// <summary>
        /// 蜘蛛队长开始工作时间
        /// </summary>
        public static DateTime SpiderCaptainStartWorkTime { get { return SpiderCaptain.StarTime; } }
        /// <summary>
        /// 蜘蛛队长结束工作时间
        /// </summary>
        public static DateTime SpiderCaptainEndWorkTime { get { return SpiderCaptain.EndTime; } }
        /// <summary>
        ///  获取蚂蚁剩余完成的任务数量
        /// </summary>
        /// <returns></returns>
        public static int RemainAntTask { get { return AntCaptain.RemainTaskCount; } }
        /// <summary>
        ///  获取蚂蚁已完成任务数量
        /// </summary>
        /// <returns></returns>
        public static int AntFinishedTaskCount{ get { return AntCaptain.FinishedTaskCount; } }
        /// <summary>
        ///  获取蚂蚁已成功完成任务数量
        /// </summary>
        /// <returns></returns>
        public static int AntSuccessFinishedTaskCount{ get { return AntCaptain.SuccessFinishedTaskCount; } }
        /// <summary>
        ///  获取当前的WebBrowser
        /// </summary>
        /// <returns></returns>
        public static WebBrowser GetCurrentWebBrowser { get { return Spider.Spiders.Spider.WebBrowser; } }
        /// <summary>
        ///  获取QQ空间当前的WebBrowser
        /// </summary>
        /// <returns></returns>
        public static Image GetQqZoneSpriderVerifyCodeImage { get { return QqSprider.VerifyCodeImage; } }
        /// <summary>
        ///  获取QQ微博当前的WebBrowser
        /// </summary>
        /// <returns></returns>
        public static Image GetQqWbSpriderVerifyCodeImage { get { return QqWBSprider.VerifyCodeImage; } }
        /// <summary>
        /// 添加Url任务
        /// </summary>
        /// <param name="url"></param>
        public static void AddUrlTask(string url)
        {
            if (_task.ContainsKey(url))
            {
                _task[url] = String.Empty;
            }
            else
            {
                _task.Add(url, String.Empty);
            }
        }

        /// <summary>
        /// 设置Url任务的当前状态
        /// </summary>
        /// <param name="url"></param>
        /// <param name="state"></param>
        public static void SetUrlTaskState(string url, string state)
        {
            if (_task.ContainsKey(url))
            {
                _task[url] += state + "[" + DateTime.Now.ToString("m:s") + "]→";
            }
            else
            {
                _task.Add(url, state + "[" + DateTime.Now.ToShortTimeString() + "]→");
            }
        }
        
        /// <summary>
        /// 清空任务记录
        /// </summary>
        public static void ClearMonitorTask()
        {
            _task = new Dictionary<string, string>();
        }
        /// <summary>
        /// 获取Url队列中任务的数量
        /// </summary>
        /// <returns></returns>
        public static int GetTodoTaskCount()
        {
            return ToDo.Length;
        }
        /// <summary>
        /// 获取已经访问过的数量
        /// </summary>
        /// <returns></returns>
        public static int GetVisitedCount()
        {
            DicVisited visited = new DicVisited();
            return visited.GetCount();
        }
        /// <summary>
        /// 获取仓库队列中任务的数量
        /// </summary>
        /// <returns></returns>
        public static int GetPageRepositoryTaskCount()
        {
            return PageRepository.Length;
        }

        /// <summary>
        /// 获取当前Url任务列表的状态
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetAllUrlTaskMonitorState()
        {
            return _task;
        }
    }
}
